/******************************************************************
 * Created by: GOh CHee Hong                                      
 * Date of Creation: 1/Jan/2009            
 
 *                                                              
 ******************************************************************/
/*
 * FeedExpress
 * (C) Copyright 2003 Jeppe Cramon (jeppe@cramon.dk)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Disclaimer:
 * -----------
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied. The entire risk as to the
 * quality and performance of the software is with you. Should the
 * software prove defective, you assume the cost of all necessary
 * servicing, repair, or correction. In no event shall the author,
 * copyright holder, or any other party who may redistribute the
 * software be liable to you for damages, including any general,
 * special, incidental, or consequential damages arising out of
 * the use or inability to use the software (including, but not
 * limited to, loss of data, data being rendered inaccurate, loss of
 * business profits, loss of business information, business
 * interruptions, loss sustained by you or third parties, or a
 * failure of the software to operate with any other software) even
 * if the author, copyright holder, or other party has been advised
 * of the possibility of such damages. 
 * 
 */
using System;
using System.Collections.Generic;
using System.Text;
using Pbdms.Common.DataObject;
using System.Threading;
using System.Timers;
namespace Pbdms.BusinessLogic.RSS
{
    [Serializable]
    public class Subscription : Pbdms.Common.Interfaces.ICacheable
    {
        //meta info about a feed
        public Common.DataObject.SubscriptionInfo info;
        public string Key;
        [NonSerialized] public FeedManager mFeedManager;
        //auto refresh timer
        [NonSerialized] private System.Timers.Timer mTimer = new System.Timers.Timer();
        public Subscription()
        {
            mFeedManager = new FeedManager(this);
            info = new Pbdms.Common.DataObject.SubscriptionInfo();
            mTimer.Elapsed += new ElapsedEventHandler(OnTimer);
            setTimer();
        }
        ~Subscription()
        {

            if (mTimer != null)
            {
                mTimer.Elapsed -= new ElapsedEventHandler(OnTimer);
            }
        }
        public Subscription(SubscriptionInfo info, BusinessLogic.Network.NetworkController networkCtrller)
        {
            mFeedManager = new FeedManager(this);
            mFeedManager.networkCtrller = networkCtrller;
            this.info = info;

            Key = info.FriendID+ " " + info.Channel;
            mTimer.Elapsed += new ElapsedEventHandler(OnTimer);
            setTimer();
        }
        public string cacheKey
        {
            get { return Key; }
        }
        //set timer
        private void setTimer()
        {
            //1 min = 60000mill sec
            //mTimer.Interval = 60000;
            mTimer.Interval = 60000;
            mTimer.Enabled = true;
          
        }
        //return feedmanager
        public FeedManager feedManager
        {
            get { return mFeedManager; }
        }
        public void OnTimer(object source, ElapsedEventArgs e)
        {
            if (mFeedManager.networkCtrller == null)
            {
                return;
            }
            Buddy buddy=mFeedManager.networkCtrller.friendsControl.getAFriendByKeyID(info.FriendID);
            if (buddy != null)
            {
                if (buddy.IpAdd != null)
                {
                    mFeedManager.refreshFeeds();
                }
            }
        }
        //save cache
        public void saveCompleteFeedCache()
        {
            mFeedManager.saveCompleteFeedCache();
        }
    }
}
